<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="Name" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.state" placeholder="State" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.completed" placeholder="completed" filterable clearable style="width: 130px" class="filter-item">
        <el-option label="all" :value="undefined" />
        <el-option label="completed" :value="true" />
        <el-option label="uncompleted" :value="false" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.keyPath" class="filter-item" @change="tableKey = tableKey + 1">keyPath</el-checkbox>
        <el-checkbox v-model="showRows.headUrl" class="filter-item" @change="tableKey = tableKey + 1">headUrl</el-checkbox>
        <el-checkbox v-model="showRows.origin" class="filter-item" @change="tableKey = tableKey + 1">origin</el-checkbox>
        <el-checkbox v-model="showRows.state" class="filter-item" @change="tableKey = tableKey + 1">state</el-checkbox>
        <el-checkbox v-model="showRows.tmpPath" class="filter-item" @change="tableKey = tableKey + 1">tmpPath</el-checkbox>
        <el-checkbox v-model="showRows.savePath" class="filter-item" @change="tableKey = tableKey + 1">savePath</el-checkbox>
        <el-checkbox v-model="showRows.completed" class="filter-item" @change="tableKey = tableKey + 1">completed</el-checkbox>
        <el-checkbox v-model="showRows.synthesized" class="filter-item" @change="tableKey = tableKey + 1">synthesized</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>

      <el-button v-waves class="filter-item" type="primary" plain icon="el-icon-plus" @click="handleCreate" />
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" @sort-change="sortChange">
      <el-table-column align="center" label="ID" width="80" prop="id" sortable="custom" />
      <el-table-column header-align="center" label="Name" prop="name" show-overflow-tooltip>
        <template slot-scope="scope">
          <text-copy :value="scope.row.name" />
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column header-align="center" label="m3u8Path" prop="m3u8Path" show-overflow-tooltip>
        <template slot-scope="scope">
          <text-copy :value="scope.row.m3u8Path" />
          {{ scope.row.m3u8Path }}
        </template>
      </el-table-column>
      <el-table-column v-if="showRows.keyPath" header-align="center" label="keyPath" prop="keyPath" show-overflow-tooltip />
      <el-table-column v-if="showRows.headUrl" header-align="center" label="headUrl" prop="headUrl" show-overflow-tooltip />
      <el-table-column v-if="showRows.origin" header-align="center" label="origin" prop="origin" show-overflow-tooltip />
      <el-table-column v-if="showRows.state" header-align="center" label="state" prop="state" show-overflow-tooltip />
      <el-table-column v-if="showRows.tmpPath" header-align="center" label="tmpPath" prop="tmpPath" show-overflow-tooltip />
      <el-table-column v-if="showRows.savePath" header-align="center" label="savePath" prop="savePath" show-overflow-tooltip />

      <el-table-column v-if="showRows.completed" align="center" label="completed">
        <template slot-scope="scope">
          <span v-if="scope.row.completed" class="success">completed</span>
          <span v-else class="waring">uncompleted</span>
        </template>
      </el-table-column>
      <el-table-column v-if="showRows.synthesized" align="center" label="synthesized">
        <template slot-scope="scope">
          <span>{{ scope.row.synthesized }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" prop="modify_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="Actions" width="120">
        <template slot-scope="scope">
          <router-link :to="{name: 'EditTSData', params: scope.row }">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain />
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="handleDelete(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, deleteTsData } from '@/api/util/ts-data'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { mapGetters } from 'vuex'
import TextCopy from '@/views/components/TextCopy.vue'

export default {
  name: 'TSList',
  components: { Pagination, TextCopy },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      showRows: {
        keyPath: true,
        headUrl: true,
        origin: false,
        state: true,
        tmpPath: false,
        savePath: false,
        completed: true,
        synthesized: false
      },
      list: null,
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        uid: undefined,
        name: undefined,
        state: undefined,
        completed: false,
        current: 1,
        size: 10,
        asc: undefined,
        desc: ['id']
      }
    }
  },
  computed: {
    ...mapGetters(['uid'])
  },
  created() {
    console.log('ts')
    this.listQuery.uid = this.uid
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery)
        .then(response => {
          this.list = response.result.list
          this.total = response.result.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    },
    handleCreate() {
      this.$router.push({ name: 'CreateTSData' })
    },
    handleDelete(id) {
      this.$confirm('Confirm delete record?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        deleteTsData(id).then(response => {
          console.log(response)
          this.$message({
            type: 'success',
            message: 'deleted successfully!'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
